package com.zd.controller;

import com.zd.domain.GoodState;
import com.zd.domain.Result;
import com.zd.mapper.GoodStateMapper;
import com.zd.service.goodstate.GoodStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/goodState")
public class GoodStateController {

    @Autowired
    private GoodStateService goodStateService;
    @Autowired
    private GoodStateMapper goodStateMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 支付功能
     *
     * @param userId
     * @param goodId
     * @return
     */
    @CrossOrigin
    @GetMapping("/updateIsBuy")
    public Result updateGoodIsBuy(@RequestParam("userId") int userId,
                                  @RequestParam("goodId") int goodId) {

        //先去查找有没有条信息，有的话直接返回来，没有的话创建一个
        GoodState goodState = goodStateService.findGoodState(userId, goodId);
        //查找回来之后在进行修改
        int influenceRows = goodStateService.updateGoodIsBuy(goodState.getUserId(), goodState.getGoodId());

        Result result = new Result();

        if (influenceRows == 0) {
            result.setCode(411);
            result.setMsg("支付失败");
        } else {
            result.setCode(200);
            result.setMsg("支付成功~~");
        }
        return result;
    }

    /**
     * 修改收藏状态
     *
     * @param userId
     * @param goodId
     * @return
     */
    @CrossOrigin
    @GetMapping("/updateIsCollect")
    public Result updateGoodIsCollect(@RequestParam("userId") int userId,
                                      @RequestParam("goodId") int goodId) {
        //先去查找有没有条信息，有的话直接返回来，没有的话创建一个
        GoodState goodState = goodStateService.findGoodState(userId, goodId);
        //查找回来之后在进行修改
        int influenceRows = goodStateService.updateGoodIsCollect(goodState.getUserId(), goodState.getGoodId());

        Result result = new Result();

        if (influenceRows == 0) {
            result.setCode(411);
            result.setMsg("收藏失败");
        } else {
            result.setCode(200);
            result.setMsg("已经成功加入收藏夹了~");
        }
        return result;
    }

    /**
     * 查找是否存在用户和课程关系
     *
     * @param userId
     * @param goodId
     * @return
     */
    @CrossOrigin
    @GetMapping("findGoodState")
    public GoodState findGoodState(int userId, int goodId) {
        GoodState goodState;
        while (true) {
            goodState = goodStateService.findGoodState(userId, goodId);

            if (goodState == null) {
                //没有找到的话创建一个
                goodStateService.saveGoodState(userId, goodId);
            }
            goodState = goodStateService.findGoodState(userId, goodId);
            break;
        }
        return goodState;
    }

    @CrossOrigin
    //新增
    @GetMapping("saveGoodState")
    public int saveGoodState(int userId, int goodId) {

        int influenceRows = goodStateService.saveGoodState(userId, goodId);

        return influenceRows;
    }

    /**
     * 更新用户视频或者电子书的总时长或者总章节
     *
     * @param goodId
     * @param totalTime
     * @return
     */
    @CrossOrigin
    @GetMapping("updateTotalDuration")
    public Result updateTotalDuration(@RequestParam("goodId") int goodId,
                                      @RequestParam("totalTime") String totalTime) {

        Result result = new Result();

        int influenceRows = goodStateService.updateTotalDuration(goodId, totalTime);

        if (influenceRows == 0) {
            result.setCode(411);
            result.setMsg("更新失败");
        } else {
            result.setCode(200);
            result.setMsg("更新成功");
        }

        return result;
    }

    /**
     * 更新某用户最后一次播放视频，或者观看章节的时长或者进度
     *
     * @param userId
     * @param currentTime
     * @param goodId
     * @return
     */
    @CrossOrigin
    @GetMapping("updateCurrentTime")
    public Result updateCurrentTime(@RequestParam("userId") int userId,
                                    @RequestParam("currentTime") String currentTime,
                                    @RequestParam("goodId") int goodId) {

        Result result = new Result();

        int influenceRows = goodStateService.updateCurrentTime(goodId, userId, currentTime);

        if (influenceRows == 0) {
            result.setCode(411);
            result.setMsg("未找到数据，更新失败！~");
        } else {
            result.setCode(200);
            result.setMsg("更新成功！~");
        }

        return result;
    }

    /**
     * 查询用户与课程之间的关系数据
     * @param userId
     * @param goodId
     * @return
     */
    @CrossOrigin
    @GetMapping("findGStateByUIdGId")
    public Result<GoodState> findGStateByUIdGId(@RequestParam("goodId") int goodId,
                                                @RequestParam("userId") int userId
                                                ) {
        Result<GoodState> result = new Result<>();

        //先去查找
        GoodState goodState = goodStateService.findGStateByUIdGId(goodId, userId);
        if (goodState == null) {
            //没有找到的话创建一个
            int influenceRows = goodStateService.saveGoodState(userId, goodId);
            if (influenceRows == 1){
                //再去查找赋值给goodState
                GoodState userGoodState = goodStateMapper.findGoodState(userId, goodId);
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(userGoodState);
            }else {
                result.setCode(411);
                result.setMsg("查询失败");
            }
        }else {
            //找到的话再返回
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(goodState);
        }
        return result;
    }
}
